<template>
  <div class="el-div">
    <div class="el-div el-s">
      <el-input
        v-model="input"
        placeholder="请输入用例名称"
        size="mini"
      ></el-input>
    </div>
    <div class="el-div">
      <el-button
        type="primary"
        icon="el-icon-search"
        size="mini"
        @click="search()"
      >搜索
      </el-button
      >
    </div>
    <div class="el-div">
      <el-button @click="cz" size="mini">重置</el-button>
    </div>
    <div class="el-div">
      <el-button
        type="primary"
        @click="
          dialogFormVisible = true;
          tjxm();
        "
        size="mini"
      >添加用例
      </el-button
      >
      <el-dialog title="添加用例" :visible.sync="dialogFormVisible" width="30%">
        <el-form>
          <el-form-item label="用例名称：" :label-width="formLabelWidth">
            <el-input
              v-model="name"
              autocomplete="off"
              placeholder="请输入用例名称"
              size="mini"
            ></el-input>
          </el-form-item>
          <el-form-item label="用例描述：" :label-width="formLabelWidth">
            <el-input
              v-model="remark"
              autocomplete="off"
              placeholder="请输入用例描述"
              size="mini"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" size="mini"
          >取 消
          </el-button
          >
          <el-button type="primary" @click="dj()" size="mini">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-table :data="categoryList.data.list" border size="mini">
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="用例名称"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="remark"
        label="用例描述"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="300"
        align="center"
        :formatter="formatter"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
          >编辑
          </el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total,sizes,prev,pager,next,jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="categoryList.data.pageSize"
      :current-page="categoryList.data.pageNumber"
      :total="categoryList.data.total"
      style="float: right; margin-top: 10px; margin-bottom: 10px"
      :small="true"
    >
    </el-pagination>
  </div>
</template>

<script>
  import axios from "axios";

  export default {
    watch: {
      projectId: function (newProjectId) {
        this.$testCase
          .tPage({pageNum: 1, pageSize: 10, projectId: this.projectId})
          .then((res) => {
            var result = res;
            if (res.code == 200) {
              this.categoryList = res;
              console.log(res);
            }
          })
          .catch((res) => {
            console.log(res);
          });
      },
    },
    computed: {
      projectId: function () {
        return this.$store.getters.getProjectId;
      },
    },
    created() {
      this.getTestCasePage({pageNum: 1, pageSize: 10, projectId: this.projectId})
    },
    data() {
      return {
        input: "",
        ta: 50,
        categoryList: {
          data: {
            list: [
              {
                id: "id",
                name: "name",
                remark: "remark",
                createTime: "createTime",
              },
            ],
            pageSize: 10,
            pageNumber: 1,
            total: 10,
          },
        },
        dialogFormVisible: false,
        name: "",
        remark: "",
        formLabelWidth: "100px",
      };
    },
    methods: {
      search() {
        this.getTestCasePage({
          pageNum: this.categoryList.data.pageNum,
          pageSize: this.categoryList.data.pageSize,
          projectId: this.projectId,
          name: this.input,
        })
      },
      formatter(row, column) {
        return this.$dateFomat.timestampToTime(row.createTime * 1000);
      },
      getTestCasePage(p) {
        this.$testCase
          .tPage(p)
          .then((res) => {
            var result = res;
            if (res.code == 200) {
              this.categoryList = res;
              console.log(res);
            }
          })
          .catch((res) => {
            console.log(res);
          });
      },
      tjxm() {
        this.name = "";
        this.remark = "";
      },
      cz() {
        this.input = "";
      },
      dj() {
        this.$testCase
          .tAdd({
            projectId: this.projectId,
            name: this.name,
            remark: this.remark,
          })
          .then((res) => {
            if (res.code == 200) {
              this.dialogFormVisible = false;
              this.getTestCasePage({
                pageNum: this.categoryList.data.pageNum,
                pageSize: this.categoryList.data.pageSize,
                projectId: this.projectId,
              })
            } else {
              this.$message.error(res.message);
            }
          })
          .catch((res) => {
            console.log(res);
          });
      },
      handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.getTestCasePage({pageNum: 1, pageSize: val, projectId: this.projectId})
      },
      handleCurrentChange(val) {
        this.getTestCasePage({
          pageNum: val,
          pageSize: this.categoryList.data.pageSize,
          projectId: this.projectId,
        })
      },
      handleEdit(index) {
        let id = this.categoryList.data.list[index].id;
        console.log(id);
        this.$router.push({path: "/StepPage", query: {id: id}});
      },
      handleDelete(index) {
        let id = this.categoryList.data.list[index].id;
        this.$testCase
          .tDelete({id: id})
          .then((res) => {
            console.log(res);
            this.getTestCasePage({
              pageNum: this.categoryList.data.pageNum,
              pageSize: this.categoryList.data.pageSize,
              projectId: this.projectId,
            });
          })
          .catch((res) => {
            console.log(res);
          });
      },
    },
  };
</script>
<style>
  .el-div {
    float: left;
    margin-left: 20px;
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .el-s {
    margin-left: 0px;
  }

  .el-col {
    border-radius: 4px;
  }
</style>
